package com.yy.wga.common.core.dto;

import com.alibaba.fastjson2.JSONObject;
import com.yy.wga.common.core.enmus.EResponseCode;
import com.yy.wga.common.core.enmus.ResponseCode;
import lombok.Data;

/**
 * 接口返回公共类
 * @param <T>
 */
@Data
public class Result<T> {
    private String code;
    private String message;
    private boolean success;
    private T data;

    public Result() {
        this.setResult(EResponseCode.FAIL);
        this.setSuccess(false);
    }

    public Result(String message) {
        this();
        this.message = message;
    }

    public Result(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(ResponseCode e) {
        this.setResult(e);
    }

    public Result<T> resultFail() {
        this.setResult(EResponseCode.FAIL);
        this.setSuccess(false);
        return this;
    }

    public Result<T> resultSuccess() {
        this.setResult(EResponseCode.SUCCESS);
        this.setSuccess(true);
        return this;
    }

    public void setResult(ResponseCode e) {
        this.code = String.valueOf(e.getCode());
        this.message = e.getDescribe();
    }

    public Result<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 根据状态返回一个Result
     *
     * @param e 状态码
     * @return 根据这个状态码生成的result
     */
    public static <T> Result<T> status(ResponseCode e) {
        return new Result<>(e);
    }

    /**
     * 返回成功,并且带上一条消息
     *
     * @param message 需要带回的消息
     * @return 根据消息生成的result
     */
    public static <T> Result<T> success(String message) {
        Result<T> result = new Result<>();
        result.resultSuccess();
        result.setMessage(message);
        return result;
    }

    /**
     * 返回成功
     *
     * @return 表示成功的result
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.resultSuccess();
        return result;
    }


    /**
     * 返回成功,并且带回所需要的数据
     *
     * @param obj 需要带回的数据
     * @return 成功, 并且带有返回数据的result
     */
    public static <T> Result<T> success(T obj) {
        Result<T> result = new Result<T>();
        return result.resultSuccess().setData(obj);
    }


    /**
     * 返回失败
     *
     * @return 表示失败的result
     */
    public static <T> Result<T> fail() {
        Result<T> result = new Result<T>();
        return result.resultFail();
    }

    /**
     * 返回失败,并且带上错误信息,默认的错误码是"1111"
     *
     * @param message 失败所包含的信息
     * @return 失败, 并且含有错误信息的result
     */
    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<>();
        result.resultFail();
        result.setMessage(message);
        return result;

    }

    /**
     * 返回失败
     *
     * @param code
     * @param message
     * @return
     */
    public static <T> Result<T> fail(String code, String message) {
        Result<T> result = new Result<>();
        result.resultFail();
        result.setCode(code);
        result.setMessage(message);
        return result;

    }

    /**
     * 返回失败,并且指定错误信息和错误码
     *
     * @param e 错误码
     * @return 失败, 并且包含指定错误信息和错误码的result
     */
    public static <T> Result<T> fail(ResponseCode e) {
        Result<T> result = new Result<>();
        result.setResult(e);
        return result;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}